import Vue from 'vue'
import store from './store';
import Router from 'vue-router'
Vue.use(Router)
import { Message } from 'iview';

// VUE router 导航重复点击报错的问题解决方案
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

/**
 * 登录后的子路由
 */
const homeChildren = [{
	path: '/UserList',
	name: 'UserList',
	component: () => import('@/views/User/List'), meta: {
		requireAuth: true, // 表示进入这个路由是需要登录的
	},
},

{
	path: '/Roles',
	name: 'Roles',
	component: () => import('@/views/Rights/Role'), meta: {
		requireAuth: true, // 表示进入这个路由是需要登录的
	},
},
{
	path: '/Me',
	name: 'Me',
	component: () => import('@/views/User/Me'),
	meta: {
		requireAuth: true, // 表示进入这个路由是需要登录的
	},

},
{
	path: '/Menu',
	name: 'Menu',
	component: () => import('@/views/menu/List'),
	meta: {
		requireAuth: true, // 表示进入这个路由是需要登录的
	},
}
];

/**
 * 总的路由
 */
const routes = [{
	path: '/login',
	name: 'login',
	meta: {
		title: '登录',
	},
	component: () => import('@/views/User/Login')
},
{
	path: '/',
	name: 'home',
	component: () => import('@/components/Layout'),
	children: homeChildren
}
];

const router = new Router({
	routes
})

router.beforeEach((to, from, next) => {
	if (to.meta.requireAuth) {
		let user = store.state.user.Password;
		if (user === undefined) {
			next({
				path: '/login'
			})
		}
		next();
	} else {
		next()
	}
})
export default router;
